package com.dstz.org.core.mapper;

import com.dstz.base.mapper.AbBaseMapper;
import com.dstz.org.core.entity.AccountOrg;
import com.dstz.org.core.entity.StockInfo;
import com.dstz.org.core.entity.YanShouInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author gaobo
 * @since 2025-03-04
 */
@Mapper
public interface AccountOrgMapper extends AbBaseMapper<AccountOrg> {

    @Select("select b.amount from org_group a LEFT JOIN (select sum(amount) as amount ,org_id from account_org group by org_id) b " +
            " on a.id_=b.org_id  where org_id=#{orgId} ")
    Double getDeptBalance(String orgId);

    @Select("SELECT * FROM `account_org` where org_id=#{orgId} and flag='C' and status<>'-1' order by CREATE_time_ ")
    List<AccountOrg> listDeptBill(int orgId);
    @Select("<script> select  d.id as id, d.name, d.count as total, ifnull( c.used, 0 ) AS used,( d.count - ifnull( c.used, 0 ) ) AS stocks  from " +
            "(SELECT b.id_ as id, b.name,a.count FROM (SELECT sum(counts) AS count,assetId FROM `zcgl_zccw` GROUP BY assetId) a LEFT JOIN zcgl_zcxx b ON a.assetId = b.id_) d left join (SELECT sum(sl) AS used, name FROM `zcgl_lldsb` GROUP BY name ) c ON c.NAME = d.id" +
            " <where>" +
            "<if test =\"keyword !=null and keyword != ''\">  d.name like  CONCAT ('%',#{keyword},'%')" +
            "</if> " +
            "<if test =\"id !=null and id != ''\">  d.id =  #{id}" +
            "</if> " +
            " </where>" +

           " ORDER BY stocks ASC </script>")
    List<StockInfo> listStockInfo(@Param("keyword") String keyword, @Param("id")String id);
    @Select("<script>select name,dj,dw,sl,amount,bz from zcgl_cgsb_dz d" +
            " <where>" +
            "<if test =\"id !=null and id != ''\">  d.cgsqId =  #{id} and d.status='0'"+
            "</if> " +
            " </where>" +
            "</script>")
    List<YanShouInfo> getChildData(@Param("id")String id);
}
